package com.example.question.dp.dp5;


public class Code03_918 {
    public static void main(String[] args) {
        Code03_918 code = new Code03_918();
        int i = code.maxSubarraySumCircular(new int[]{5, -3, 5});
//        int i = code.maxSubarraySumCircular(new int[]{1, -2, 3, -2});
        System.out.println(i);
    }

    /**
     * 环形问题转化为数组问题
     */
    public int maxSubarraySumCircular(int[] nums) {
        int[] newNums = new int[nums.length * 2];

        for (int i = 0; i < nums.length * 2; i++) {
            if (i > nums.length - 1) {
                newNums[i] = nums[i - nums.length];
            } else {
                newNums[i] = nums[i];
            }
        }

        // 滑动窗口里面选择
        // 以i结尾
        int max = Integer.MIN_VALUE;
//        int i1 = nums.length > 100 ? 2 : nums.length;
        for (int i = 1; i < nums.length; i++) {
            int[] dp = new int[newNums.length];
            dp[0] = nums[i];
            max = Math.max(dp[0], max);
            int len = nums.length;
            for (int j = i + 1; j < (i + len); j++) {
                dp[j - i] = Math.max(dp[j - i - 1] + newNums[j], newNums[j]);
                max = Math.max(max, dp[j - i]);
            }
        }
        return max;
    }
}
